package com.rgs.gateway.web.controller;

import com.rgs.gateway.common.pojo.Result;
import com.rgs.gateway.web.common.component.RgsRedisRouteDefinitionRepository;
import com.rgs.gateway.web.service.IGatewayRouteService;
import com.rgs.gateway.web.service.IRouteService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author RenGaoshuai
 * @date 2024/6/28 11:05
 * @description
 */
@RestController
@RequestMapping("/gateway/routes")
public class GatewayController {

    @Autowired
    private IGatewayRouteService iGatewayRouteService;
    @Autowired
    private IRouteService iRouteService;
    @Autowired
    private RgsRedisRouteDefinitionRepository rgsRedisRouteDefinitionRepository;


    @ApiOperation(value = "重载资源")
    @GetMapping(value = "/all/overload")
    public Result overload() {

        //从数据库中重载路由资源
        boolean overload = iGatewayRouteService.overload();
        if (overload) {

            //加载到DefinitionMap中
            iRouteService.loadRoute();

            //将路由刷新到gateway框架中生效
            rgsRedisRouteDefinitionRepository.getRouteDefinitions();
        } else {
            Result.fail("网关路由重载失败，请稍后重试！");
        }
        return Result.success();
    }
}
